package cn.com.huzo.service.workflow.impl;

import cn.com.huzo.model.entities.workflow.ApprovalRuleMrpEntity;
import huzofw.service.impl.GenericManagerImpl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.com.huzo.repository.workflow.ApprovalRuleMrpRepository;
import cn.com.huzo.service.workflow.ApprovalRuleMrpManager;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import huzofw.model.entities.paginator.Paginator;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.collections4.MapUtils;

/**
 * 调达表审核规则管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("ApprovalRuleMrpManager")
public class ApprovalRuleMrpManagerImpl extends GenericManagerImpl<ApprovalRuleMrpEntity, Long>
        implements ApprovalRuleMrpManager {

    @Resource(name = "ApprovalRuleMrpRepository")
    private ApprovalRuleMrpRepository _dao;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> rtnValue = new HashMap();

        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<ApprovalRuleMrpEntity> itemList = _dao.fetchForPage(criterion);
            Paginator<ApprovalRuleMrpEntity> paginator = new Paginator<>(itemList, offset, MapUtils.getInteger(criterion, "Limit"), total);

            rtnValue.put("TotalRecordCount", paginator.getTotal());
            rtnValue.put("TotalPages", paginator.getTotalPages());
            rtnValue.put("PageIndex", paginator.getPageIndex());
            rtnValue.put("DataList", paginator.getItemList());
            rtnValue.put("Success", true);
        } else {
            rtnValue.put("Success", false);
        }

        return rtnValue;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchUpdate(Map<String, Object> criterion) throws Exception {
        int _rowsAffected1 = 0;
        int _rowsAffected2 = 0;

        ObjectMapper mapper = new ObjectMapper();
        List<ApprovalRuleMrpEntity> insertList = mapper.convertValue(MapUtils.getObject(criterion, "insertList"), new TypeReference<List<ApprovalRuleMrpEntity>>() {
        });
        List<ApprovalRuleMrpEntity> updateList = mapper.convertValue(MapUtils.getObject(criterion, "updateList"), new TypeReference<List<ApprovalRuleMrpEntity>>() {
        });
        Map<String, Object> _pageData = (Map<String, Object>) criterion.get("pageData");

        if (!insertList.isEmpty()) {
            _rowsAffected1 = _dao.batchInsert(insertList);
        }
        if (!updateList.isEmpty()) {
            _rowsAffected2 = _dao.batchUpdate(updateList);
        }
        // 写日志
        if (_rowsAffected1 > 0) {
        }
        if (_rowsAffected2 > 0) {
        }

        return fetchForPage(_pageData);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        _dao.delete(MapUtils.getLong(criterion, "RuleId"));
        return fetchForPage(criterion);
    }
}
